#include <stdio.h>
#include <stdbool.h>
#define Size 15
#include <string.h>

void IntiBoard(char (*Board)[Size])
{
   for(int i = 0 ; i < Size;i++)
   {
      for(int j = 0;j< Size;j++)
      {
         Board[i][j] = '-';
      }
   }
}

void PrintBoard(char (*Board)[Size])
{
    printf("    ");
    for(int i = 0; i<Size;i++)
    {
        printf("%4d ",i+1);
   
    }
    printf("\n");
    for(int i=0;i<Size;i++)
    {
        printf("%4d ",i+1);
        for(int j=0;j<Size;j++)
       {
          printf("%4c ",Board[i][j]);
       }
       printf("\n");
    }

}
bool MakeMove(char (*Board)[Size],int row,int colum,char player)
{
    if(row < 0 || row >= Size || colum < 0 || colum >= Size)
    {
        printf("下的位置是非法位置，请重新输入！");
        return false;
    }
    if(Board[row][colum] != '-')
    {
        printf("当前位置已经有棋子了，请重新输入！\n");
        return false;
    }
     Board[row][colum] = player;
     return true;
 }
//判断是否是平局
 bool CheckIsDraw(char (*Board)[Size],char player)
 {
    for(int i = 0 ; i< Size;i++)
    { 
        for(int j = 0;j<Size;j++)
        {
            if(Board[i][j] == '-')
                return false;
        }
    }
    return true;//平局
 }
 //判断是否胜利
 bool CheckIsWin(char(*Board)[Size],int row,int column,char player)
 {
    //向量数组
    int direction[4][2] = 
    {
        {1,0},
        {0,1},
        {1,1},
        {-1,1}
    };
    //四个方向
    for(int i = 0; i < 4;i++)
    {
        //计算这个方向上棋子的数量
        int count = 1;
        int dx = row + direction[i][0];
        int dy = column + direction[i][1];
        while (dy >= 0 && dx < Size && dy >= 0 && dy<Size && player == Board[dx][dy])
        {
            count++;
            dx = dx + direction[i][0];
            dy = dy + direction[i][1];
            if(count == 5)
            {
                return true;
            }
        }
        dx = row - direction[i][0];
        dy = column - direction[i][1];
        while (dy >= 0 && dx < Size && dy >= 0 && dy < Size && player == Board[dx][dy])
        {
            count++;
            dx = dx - direction[i][0];
            dy = dy - direction[i][i];
            if(count == 5)
            {
                return true;
            }
        }
    }
    return false;

 }
int main(int argc,char *argv[])
{
    if(argc != 3)
    {
        printf("输入的参数数量错误！\n");
        return -1;
    }
    if(strcmp(argv[1],"admin") == 0 && strcmp(argv[2],"123456") == 0 )
    {
        printf("登录成功！欢迎来到五子棋\n");
    }
    else
    {
       printf("帐号或密码错误！\n");
       return -1;
    }

    char Board[Size][Size];
    IntiBoard(Board);
    //PrintBoard(Board);
    
    char player = 'x';
   while (1)
   {
     PrintBoard(Board);
     int row = 0;
     int colum = 0;
     printf("请玩家|%c|下棋:",player);
     //从终端读取输入
     scanf("%d %d",&row,&colum);
     if(MakeMove(Board,row - 1,colum - 1,player) == false)
     {
         continue;
     }
     if(CheckIsWin(Board,row - 1,colum - 1,player) == true)
     {
        PrintBoard(Board);
        printf("恭喜|%c|玩家获胜！\n",player);
        break;
     }
     if(CheckIsDraw(Board,player) == true)
     {
        PrintBoard(Board);
        printf("平局！\n");
        break;
     }
     player = (player == 'x') ? 'o' : 'x';
   }

     return 0;
}